import { useCallback, useEffect } from 'react';
import { clearObject } from 'utils/index';
import { useHttp } from 'utils/http';
import { useAsync } from 'utils/use-async';
import { Project } from 'screens/project-list/list';
import { Params } from 'react-router';
export const useProjects = (param?: Partial<Project>) => {
  const client = useHttp();
  const { run, ...result } = useAsync<Project[]>();
  const featychProjects = useCallback(() => client('projects', { data: clearObject(param || {}) }), [param, client])
  useEffect(() => {
    run(featychProjects(), {
      retry: featychProjects
    });
    //TODO 依赖项里加上callback会造成无限循环，这个和usecallback以及userMemo有关系
  }, [featychProjects, param, run]);
  return result
}
export const useEditProject = () => {
  const { run, ...asyncResult } = useAsync()
  const client = useHttp()
  const mutate = (params: Partial<Project>) => {
    return run(
      client(`projects/${params.id}`, {
        data: params,
        method: 'PATCH'
      })
    )
  }
  return { mutate, ...asyncResult }
}
export const useAddProject = () => {
  const { run, ...asyncResult } = useAsync()
  const client = useHttp()
  const mutate = (params: Partial<Project>) => {
    return run(
      client(`projects/${params.id}`, {
        data: params,
        method: 'POST'
      })
    )
  }
  return { mutate, ...asyncResult }
}